import Vue from "vue";
import Vuex from "vuex";
//导入axios
import axios from 'axios'
Vue.use(Vuex);
import { Toast } from 'vant';
Vue.use(Toast);
export default new Vuex.Store({
    state: {
        // 定义变量
        // 定义变量
        isNum: '',
        isPayment: '',
        count: 1,
        historyCity: [],
        startCity: '菏泽',
        endCity: '北京',
        isStart: false,
        isEnd: false,
        // 保存乘车人的信息
        isUser: '',
        // 存放多条乘车人信息
        passengerList: [],
        // 用户订单时的乘车人信息
        orderpassenger: [],
        week: '日一二三四五六'.charAt(new Date().getDay()),
        // weeklist:["日","一","二","三","四","五","六"],
        // 定义月
        month: new Date(
            new Date().getFullYear(),
            new Date().getMonth() + 1,
            new Date().getDate()
        ).getMonth(),
        date: new Date(
            new Date().getFullYear(),
            new Date().getMonth() + 1,
            new Date().getDate()
        ).getDate(),
        newTime: `${new Date().getMonth()+1}月${new Date().getDate()}日`,
        // 存放余票列表的数据
        ticketList: [],
        ticketParams: {},
        result: [],
        resultItem: '',
        ticketListAll: [],
        year: new Date().getFullYear(),
    },
    getters: {},
    mutations: {
        ticketParams(state, params) {
            state.ticketParams = params;
        },
        isStart(state) {
            state.isStart = true
            state.isEnd = false
        },
        isEnd(state) {
            state.isStart = false
            state.isEnd = true
        },
        actionApi(state, data) {
            // console.log(data.data);
            state.ticketList = state.ticketListAll = data.data;
        },
        retreat(state) {
            if (new Date(state.year, state.month - 1, state.date) < new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() + 15)) {
                state.date++;
                //  console.log(new Date(state.year, state.month - 1, state.date));
                // console.log(new Date(new Date().getFull(), new Date().getMonth(), new Date().getDate() + 15));
                // 到用户点击的天数超过本月的最后一天 说明已经到下月开始了， 需要把月份加1 天数 = 1
                if (state.date > new Date(state.year, state.month, 0).getDate()) {
                    // 说明已经是下月开始了
                    // state.month = state.month + 1;
                    state.month += 1;
                    if (state.month > 12) {
                        // 月份大于十二说明进入下一年， 要把年份加一
                        state.month = 1;
                        state.year += 1;
                    }
                    state.date = 1;
                    // console.log(new Date(state.year, state.month, state.date));
                }
            } else {
                Toast.fail('只能够购买15天以内的车票 ');
            }
            //console.log(new Date(state.year, state.month - 111, state.date));
            state.newTime = `${state.month}月${state.date}日`;
            // 判断当前天数大于15天提示用户超出订票范围， 用vant中的轻提示
            state.week = '日一二三四五六'.charAt(new Date(state.year, state.month - 1, state.date).getDay());
        },
        front(state) {
            if (new Date(state.year, state.month - 1, state.date) >  
            new Date(
                new Date().getFullYear(),
                new Date().getMonth(),
                new Date().getDate()
              )) 
              {
                state.date--;
                // 判断当用户的天数 小于1 说明要进入到上个月的最后一天了  需要把月份减1 天数 = 上个月的最后一天
                if (state.date < 1) {
                    // state.month = state.month - 1;
                    state.month -= 1;
                    state.date = new Date(state.year,state.month,state.date).getDate();
                }
                    if (state.month < 1) {
                        state.month = 12;
                        state.year -= 1;
                    }
            } else {
                Toast.fail('前一天已经过去了哦 ');
            }
            // 判断当前点击的天数已经是今天了，就提示用户“已经是最近一天了”
            state.newTime = `${state.month}月${state.date}日`;
            state.week = '日一二三四五六'.charAt(new Date(state.year, state.month - 1, state.date).getDay());

        },
        getFilterBtn(state) {
            // console.log("123");
            // 写筛选步骤
            // 先清空原来点过的值  每次都是当前变化后的数组进行重新遍历
            state.resultItem = '';
            // // 把当前数组中的每一项的值赋给resultItem变量，是为了一会去匹配高铁类型  高铁类型是字符串
            for (let i = 0; i < state.result.length; i++) {
                state.resultItem += state.result[i];
            }
            // // 进行数据筛选
            state.ticketList = state.ticketListAll.filter((ticket) => {
                // 判断   如果筛选项有值，显示器点，终点的余票并且满足筛选项的值去匹配当前余票中跟筛选想满足的余票  否则就显示正常的起点，终点  时间的余票
                if (state.result.length > 0) {
                    for (let i = 0; i < state.result.length; i++) {
                        return (
                            ticket.start_station.indexOf(state.startCity) != -1 &&
                            ticket.end_station.indexOf(state.endCity) != -1 &&
                            ticket.date.indexOf(state.newTime) != -1 &&
                            state.resultItem.indexOf(ticket.train_type) != -1
                        );
                    }
                } else {
                    return (
                        ticket.start_station.indexOf(state.startCity) != -1 &&
                        ticket.end_station.indexOf(state.endCity) != -1 &&
                        ticket.date.indexOf(state.newTime) != -1
                    )
                }
            }).sort((a, b) => {
                return a.start_time > b.start_time ? 1 : -1;
            });
        }

    },
    actions: {
        // isStart({ commit }) {
        //     commit("isStart")
        // },等同于
        isStart: ({ commit }) => commit('isStart'),

        isEnd: ({ commit }) => commit('isEnd'),
        ticketParams: ({ commit }, params) => {
            commit('ticketParams', params)
        },
        actionApi: ({
            commit,
            state
        }) => {
            axios.get('/api/get/ticket', {
                params: {
                    ...state.ticketParams
                }
            }).then((res) => {
                // console.log(res);
                // action只能发起请求    做异步操作
                commit('actionApi', res.data);
            })
        },
        // addAction(context, n) {
        //     // 发命令给multataion让multataion去做具体的事情
        //     context.commit("addMutation", n);
        // },
        // subAction(context, n) {
        //     // 发命令给multataion让multataion去做具体的事情
        //     context.commit("subMutation", n);
        // },
        retreat: ({ commit }) => {
            commit('retreat')
        },
        front: ({ commit }) => {
            commit('front')
        },
        getFilterBtn: ({ commit }) => { commit('getFilterBtn') }
    },
    modules: {},
});